/*
Copyright (C) 2014  Johan Degraeve

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/gpl.txt>.

*/

package helpdiabetesj2me;

import java.util.Date;
import javax.microedition.lcdui.*;

/**
 *
 * @author Johan
 */
public class ChangeSettingsForm extends Form implements CommandListener, ItemStateListener {

    static final String[] str_instellingen ={"Instellingen"};
    static final String[] str_terug ={"Ok"};
    static final String[] str_help ={"Help"};
    static final String[] str_ontb_grkh={"Hoeveel gram koolhydraten per eenheid insuline ?"};
    static final String[] str_kl={"klein"};
    static final String[] str_nor={"normaal"};
    static final String[] str_gro={"groot"};
    static final String[] str_kies_let ={"Lettergrootte"};
    static final String[] str_letop1={"De lettergrootte is gewijzigd. De applicatie zal afgesloten worden. Daarna kunt U de applicatie herstarten en zal de nieuwe lettergrootte gebruikt worden."};
    static final String[] str_help1 ={"Hoeveel gram koolhydraten mag U eten per eenheid insuline ?"};
    static final String[] str_help6 ={"Lettergrootte zal gebruikt worden in alle schermen na heropstarten van de applicatie"};
    static final String[] str_what_to_do_with_list ={"Na opstarten de aanwezige lijst verwijderen ?"};
    static final String[] str_always_delete ={"Ja indien ouder dan {1} uur"};
    static final String[] str_always_keep ={"Nooit"};
    static final String[] str_always_ask ={"Eerst Vragen"};

//can be used to store the current displayable
    Displayable curdisp;
    TextField ins_1;

    Command terug,help;
    UserInfo ui;
    ChoiceGroup lettergrootte;
    ChoiceGroup DeleteList;

    int tmp1,tmp2,tmp3;

    
    boolean changed;
    
    /** Creates a new instance of ChangeSettingsForm */
    public ChangeSettingsForm(Displayable curdisp) {
        super(str_instellingen[HelpDiabetes.languageIndex]);
        this.curdisp = curdisp;
        changed = false;
        terug = new Command(str_terug[HelpDiabetes.languageIndex],Command.BACK,0);
        this.addCommand(terug);
        help = new Command(str_help[HelpDiabetes.languageIndex],Command.HELP,0);
        this.addCommand(help);

        this.setCommandListener(this);
        ins_1 = new TextField(str_ontb_grkh[HelpDiabetes.languageIndex],((Float)HDProperties.get("Ins_1")).toString(),5,TextField.DECIMAL);
        if (ins_1.getString().compareTo("0") == 0) {
            ins_1.setString("");
        }
        this.append(ins_1);
        this.setItemStateListener(this);

        String[] tmp = {str_kl[HelpDiabetes.languageIndex],str_nor[HelpDiabetes.languageIndex],str_gro[HelpDiabetes.languageIndex]};
        lettergrootte = new ChoiceGroup(str_kies_let[HelpDiabetes.languageIndex],Choice.EXCLUSIVE,tmp ,null);
        tmp1 = ((Integer)HDProperties.get("LetterGrootte")).intValue();
        switch (tmp1) {
            case Font.SIZE_SMALL: 
                tmp2 = 0;
                break;
            case Font.SIZE_MEDIUM:
                tmp2 = 1;
                break;
            case Font.SIZE_LARGE:
                tmp2 = 2;
                break;
            default:
        }
        lettergrootte.setSelectedIndex(tmp2,true);
        this.append(lettergrootte);

        float timetokeep = (((float)(((float)HelpDiabetes.MAX_TIME_TO_KEEP_SELECTEDITEMS_IN_MINUTES)/60)) * (float)10 + (float) 0.5)/10;
        String[] tmp4 = new String [] {Utilities.replace(str_always_delete[HelpDiabetes.languageIndex],"{1}", "" + timetokeep),str_always_keep[HelpDiabetes.languageIndex],str_always_ask[HelpDiabetes.languageIndex]};
        DeleteList = new ChoiceGroup(str_what_to_do_with_list[HelpDiabetes.languageIndex], Choice.EXCLUSIVE,tmp4,null);
        if (HDProperties.askbeforedelete()) {
            DeleteList.setSelectedIndex(2,true);//always keep
        } else {
            if (HDProperties.neverdelete()) {
                DeleteList.setSelectedIndex(1,true);//always ask
            } else {
                DeleteList.setSelectedIndex(0,true);//always ask
            }
        }
        this.append(DeleteList);

        ui = new UserInfo();
        
    }

    public void commandAction(Command command, Displayable displayable) {
        String[] tmp4;
        if (command == terug) {
            
             if (changed == true) {//if any of the items changed they will all be checked, restored in the hashtable, stored in the rs
                 Float temp = Float.ZERO;
                 int temp2 = 0;
                 if (ins_1.getString().length() > 0) {
                     temp = Float.parse(ins_1.getString(),10);
                 }
                 HDProperties.put("Ins_1",new Float(temp));

                 //not used anymore but keep it here because it change the sequence of settings
                 temp = Float.ZERO;
                 HDProperties.put("Carbs_Per_Portion",temp);

                 //not used anymore but keep it here because it change the sequence of settings
                 HDProperties.put("Snelheidsfactor",new Integer(temp2));
                 
                 tmp1 = lettergrootte.getSelectedIndex();
                 switch (tmp1) {
                    case 0 : 
                        tmp2 = Font.SIZE_SMALL;
                        break;
                    case 1 :
                        tmp2 = Font.SIZE_MEDIUM;
                        break;
                    case 2 :
                        tmp2 = Font.SIZE_LARGE;
                        break;
                    default:
                 }
                 
                 //current lettergrootte is stored  in order to check if it's changed a few steps later.'
                 tmp3 = ((Integer)HDProperties.get("LetterGrootte")).intValue();
                 HDProperties.put("LetterGrootte",new Integer(tmp2));

                 //store the selected kolom
                 HDProperties.put("Kolom",new Integer(0));

                 //store the value for delete list
                 if (DeleteList.getSelectedIndex() == 0) {
                     HDProperties.setneverdelete(false);
                     HDProperties.setaskbeforedelete(false);
                 } else {
                     if (DeleteList.getSelectedIndex() == 1) {
                         HDProperties.setneverdelete(true);
                         HDProperties.setaskbeforedelete(false);
                     } else
                         HDProperties.setaskbeforedelete(true);
                 }

                 // some fields may have changed so let's close the rs
                 HDProperties.close();
                 changed = false; 
                 if (tmp3 != tmp2) {
                     (new UserInfo()).ReportAndClose(str_letop1[HelpDiabetes.languageIndex]);
                 }
             } else {
                 HelpDiabetes.midlet.getDisplay().setCurrent(curdisp);
             }
        } else if (command == help) {
            (new UserInfo()).Report(
                    str_help1[HelpDiabetes.languageIndex] + "\n"
                    + "--------------\n"
                    + str_help6[HelpDiabetes.languageIndex]);
        } 
    }
      
    public void itemStateChanged(Item item) {
        changed = true;
    }

    public void reset_ins_values() {
        ins_1.setString(((Float)HDProperties.get("Ins_1")).toString());
        if (ins_1.getString().compareTo("0") == 0) {
            ins_1.setString("");
        }

    }
}
